//
//  ViewController.swift
//  BWEmptyKit
//
//  Created by 朱旭宏 on 02/28/2022.
//  Copyright (c) 2022 朱旭宏. All rights reserved.
//

import BWUIKit
import BWEmptyKit
import BWListKit
import BWHUDKit
import BWPanelKit

extension Empty.SenarioSet {
    static func appNoData(_ handler: @escaping ()->Void) -> Empty.SenarioSet {
        return .init(senario: .noData, data: .init(styling: { emptyView in
            emptyView.label?.font = UIFont.system16
            emptyView.label?.textColor = .red
        }, image: UIImage(named: "empty_no-data"), tips: "当前没有任何数据哦~", action: .init(icon: UIImage(named: "ic_book"), title: "去书架看看", handler: handler)))
    }
    static let appNetworkError = Empty.SenarioSet(senario: .networkError, data: .init(styling: { emptyView in
        emptyView.button?.titleLabel?.font = UIFont.system16
        emptyView.button?.tintColor = .red
    }, image: UIImage(named: "empty_network-error"), tips: "吼吼，貌似你的网络有问题哦~", action: .init(icon: UIImage(named: "ic_refresh"), title: "去检查网络", handler: {
        Dialog.show(title: "去检查网络", maskViewCancellable: true)
    })))
    static func appRequestFailed(_ handler: @escaping ()->Void) -> Empty.SenarioSet {
        return .init(senario: .requestFailed, data: .init(image: UIImage(named: "empty_request-failed"), tips: "十分抱歉，数据请求失败啦~", action: .init(icon: UIImage(named: "ic_ball"), title: "重新请求", handler: handler)))
    }
    static func appCustom(_ type: CustomEmptyType, handler: (()->Void)? = nil) -> Empty.SenarioSet {
        return .init(senario: .custom(type), data: .init(styling: {
            $0.stackView?.spacing = 40
            $0.label?.textAlignment = .center
            $0.button?.imageEdgeInsets = .with(left: -2)
            $0.button?.titleEdgeInsets = .with(right: -2)
            $0.button?.contentEdgeInsets = .with(horizontal: 12, vertical: 4)
            $0.button?.layer.borderWidth = 0.5
            $0.button?.layer.borderColor = UIColor.lightGray.cgColor
            $0.button?.tintColor = UIColor.systemRed
            $0.button?.layer.cornerRadius = $0.button!.bounds.height/2
        }, image: type.icon, tips: type.tips, action: type.action(handler)))
    }
}

enum CustomEmptyType: String {
    case noOrders = "empty_no-orders"
    case noGoods = "empty_no-goods"
    case noUpdates = "empty_no-updates"
    
    var icon: UIImage? {
        return UIImage(named: rawValue)
    }
    var tips: String? {
        switch self {
        case .noOrders: return "当前没有任何订单哦，快去商城看看吧~"
        case .noGoods: return "该分类下暂时没有商品哦，换个分类试试吧~"
        case .noUpdates: return "当前文章还未发布呢，到别处逛逛吧~"
        }
    }
    var actionTitle: String? {
        switch self {
        case .noOrders: return "去商城"
        case .noGoods: return "换个分类"
        case .noUpdates: return "去贴吧"
        }
    }
    var actionIcon: UIImage? {
        switch self {
        case .noOrders: return .init(named: "ic_discount")
        case .noGoods: return .init(named: "ic_filter")
        case .noUpdates: return .init(named: "ic_pie-chart")
        }
    }
    
    func action(_ handler: (()->Void)? = nil) -> Empty.Data.Action {
        return .init(icon: actionIcon, title: actionTitle, handler: handler)
    }
}

class DemoCell: TableViewCell {
    override func bwListItemViewConfigure(_ data: Any?, indexPath: IndexPath) {
        textLabel?.text = data as? String
    }
}

class ViewController: TableViewController {
    lazy var section1 = BWListSection()
    lazy var section2 = BWListSection()
    
    override func didLoad() {
        super.didLoad()
        
        Empty.binds(
            [
                .appNoData {
                    HUD.show(.toast(.init(text: "去书架看看", position: .top)))
                },
                .appNetworkError,
                .appRequestFailed {
                    HUD.show(.toast(.init(text: "重新请求", position: .bottom)))
                },
                .appCustom(.noOrders, handler: {
                    Dialog.show(title: CustomEmptyType.noOrders.actionTitle, maskViewCancellable: true)
                }),
                .appCustom(.noGoods, handler: {
                    Dialog.show(title: CustomEmptyType.noGoods.actionTitle, maskViewCancellable: true)
                }),
                .appCustom(.noUpdates, handler: {
                    Dialog.show(title: CustomEmptyType.noUpdates.actionTitle, maskViewCancellable: true)
                }),
            ], for: listAdapter)
        
        listAdapter.data = .init(registers: [
            .init(style: .cell, class: DemoCell.self)
        ], sections: [
            section1,
            .init(items: (1...10).map{ String($0) }.map{
                .init(reuseId: DemoCell.RID, height: 44, data: $0)
            }),
            section2
        ])
    }
    
    override func layoutDidChange() {
        super.layoutDidChange()
        
        if let _ = Empty.showing(for: listAdapter) {
            Empty.reload(for: listAdapter)
        }
    }
    
    @IBAction func changeSectionEmpty(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            Empty.showHeader(for: listAdapter, section: 0, data: .init(image: UIImage(named: "empty_no-updates"), tips: "Here is my section 1 empty `Header` showing!"))
        case 1:
            Empty.showCell(for: listAdapter, section: 1, data: .init(image: UIImage(named: "empty_no-orders"), tips: "Here is my section 2 empty `Cell` showing!"))
        case 2:
            Empty.showFooter(for: listAdapter, section: 2, data: .init(image: UIImage(named: "empty_no-goods"), tips: "Here is my section 3 empty `Footer` showing!"))
        default: break
        }
    }
    
    var currentEmpty: Empty.Senario?
    
    @IBAction func changeEmpty(_ sender: UISegmentedControl) {
        let idx = sender.selectedSegmentIndex
        switch idx {
        case 0:
            currentEmpty = .noData
        case 1:
            currentEmpty = .networkError
        case 2:
            currentEmpty = .requestFailed
        default: break
        }
        Empty.show(currentEmpty!, for: listAdapter)
    }
    
    @IBAction func changeCustomEmpty(_ sender: UISegmentedControl) {
        let idx = sender.selectedSegmentIndex
        switch idx {
        case 0:
            currentEmpty = .custom(CustomEmptyType.noGoods)
        case 1:
            currentEmpty = .custom(CustomEmptyType.noOrders)
        case 2:
            currentEmpty = .custom(CustomEmptyType.noUpdates)
        case 3:
            Empty.show(.init(image: UIImage(named: "empty_rocket"), tips: "Here displays the temporary empty settings", action: .init(title: "Temp show")), for: listAdapter)
            return
        default: break
        }
        Empty.show(currentEmpty!, for: listAdapter)
    }
}

